package com.tuoyu.peopletest.utils;


import android.text.TextUtils;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 项目通用工具类(没有项目的特定对象,字段有联系)
 */
public class SCUtils {


    private static DecimalFormat decimalFormat;

    /**
     * 从 obj 获取字符串
     *
     * @param obj
     * @return
     */
    public static String getStr4Obj(Object obj) {

        return "";
    }


    /**
     * list 是否有数据
     *
     * @return
     */
    public static boolean isListHaveData(List list) {
        return list != null && list.size() > 0;
    }

    /**
     * list 是否为空集合
     *
     * @return
     */
    public static boolean isListNoData(List list) {
        return list == null || list.size() == 0;
    }


    public static String getDoubleSave2(double data) {
        if (decimalFormat == null) {
            decimalFormat = new DecimalFormat("#############.00");
        }

        if (decimalFormat.format(data).startsWith(".")) {
            return "0" + decimalFormat.format(data);
        }

        return decimalFormat.format(data);
    }

    public static String getDoubleSave3(double data) {
        if (decimalFormat == null) {
            decimalFormat = new DecimalFormat("#############.000");
        }
        if (decimalFormat.format(data).startsWith(".")) {
            return "0" + decimalFormat.format(data);
        }

        return decimalFormat.format(data);
    }

    /**
     *  转换double值
     * @param digit 位数
     * @param data
     * @return
     */
    public static double getTranDouble(int digit,double data) {
        BigDecimal bg = new BigDecimal(data);
        /**
         * 参数：
         newScale - 要返回的 BigDecimal 值的标度。
         roundingMode - 要应用的舍入模式。
         返回：
         一个 BigDecimal，其标度为指定值，其非标度值可以通过此 BigDecimal 的非标度值乘以或除以十的适当次幂来确定。
         */
        double f1 = bg.setScale(digit, BigDecimal.ROUND_HALF_UP).doubleValue();
        return f1;
    }

    /**
     * 三个字符拼接
     */
    public static String get3StrJoint(String s1, String s2, String s3) {
        StringBuffer sb = new StringBuffer();
        sb.append("");
        if (!TextUtils.isEmpty(s1)) {
            sb.append(s1);
        }
        if (!TextUtils.isEmpty(s2)) {
            sb.append(s2);
        }
        if (!TextUtils.isEmpty(s3)) {
            sb.append(s3);
        }
        return sb.toString();
    }

    /**
     * 为null, 活着 空, 返回空串
     *
     * @return
     */
    public static String getNoEmpty(String s) {
        if (TextUtils.isEmpty(s)) {
            return "";
        }
        if(s.equals("null")){
            return "";
        }
        return s;
    }

    /**
     * 通过身份证号码获取出生日期、性别、年龄
     * @param certificateNo
     * @return 返回的出生日期格式：1990-01-01 性别格式：F-女，M-男
     */
    public static Map<String, String> getBirAgeSex(String certificateNo) {
        String birthday = "";
        String age = "";
        String sexCode = "";

        int year = Calendar.getInstance().get(Calendar.YEAR);
        char[] number = certificateNo.toCharArray();
        boolean flag = true;
        if (number.length == 15) {
            for (int x = 0; x < number.length; x++) {
                if (!flag)
                    return new HashMap<String, String>();
                flag = Character.isDigit(number[x]);
            }
        } else if (number.length == 18) {
            for (int x = 0; x < number.length - 1; x++) {
                if (!flag)
                    return new HashMap<String, String>();
                flag = Character.isDigit(number[x]);
            }
        }
        if (flag && certificateNo.length() == 15) {
            birthday = "19" + certificateNo.substring(6, 8) + "-" + certificateNo.substring(8, 10) + "-"
                    + certificateNo.substring(10, 12);
            sexCode = Integer.parseInt(certificateNo.substring(certificateNo.length() - 3, certificateNo.length()))
                    % 2 == 0 ? "F" : "M";
            age = (year - Integer.parseInt("19" + certificateNo.substring(6, 8))) + "";
        } else if (flag && certificateNo.length() == 18) {
            birthday = certificateNo.substring(6, 10) + "-" + certificateNo.substring(10, 12) + "-"
                    + certificateNo.substring(12, 14);
            sexCode = Integer.parseInt(certificateNo.substring(certificateNo.length() - 4, certificateNo.length() - 1))
                    % 2 == 0 ? "F" : "M";
            age = (year - Integer.parseInt(certificateNo.substring(6, 10))) + "";
        }
        Map<String, String> map = new HashMap<String, String>();
        map.put("birthday", birthday);
        map.put("age", age);
        map.put("sexCode", sexCode);
        return map;
    }

    public static int getAgeByBirth(String birthdayStr) {
        int age = 0;
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Date bithday = format.parse(birthdayStr);

            Calendar now = Calendar.getInstance();
            now.setTime(new Date());// 当前时间

            Calendar birth = Calendar.getInstance();
            birth.setTime(bithday);

            if (birth.after(now)) {//如果传入的时间，在当前时间的后面，返回0岁
                age = 0;
            } else {
                age = now.get(Calendar.YEAR) - birth.get(Calendar.YEAR);
                if (now.get(Calendar.DAY_OF_YEAR) > birth.get(Calendar.DAY_OF_YEAR)) {
                    age += 1;
                }
            }
            return age;
        } catch (Exception e) {//兼容性更强,异常后返回数据
            return 0;
        }
    }

    public static int getAgeByBirth02(String birthdayStr) {
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Date birthday = format.parse(birthdayStr);

            //Calendar：日历
            /*从Calendar对象中或得一个Date对象*/
            Calendar cal = Calendar.getInstance();
            /*把出生日期放入Calendar类型的bir对象中，进行Calendar和Date类型进行转换*/
            Calendar bir = Calendar.getInstance();
            bir.setTime(birthday);
            /*如果生日大于当前日期，则抛出异常：出生日期不能大于当前日期*/
            if (cal.before(birthday)) {
                //throw new IllegalArgumentException("The birthday is before Now,It's unbelievable");
                return 0;
            }
            /*取出当前年月日*/
            int yearNow = cal.get(Calendar.YEAR);
            int monthNow = cal.get(Calendar.MONTH);
            int dayNow = cal.get(Calendar.DAY_OF_MONTH);
            /*取出出生年月日*/
            int yearBirth = bir.get(Calendar.YEAR);
            int monthBirth = bir.get(Calendar.MONTH);
            int dayBirth = bir.get(Calendar.DAY_OF_MONTH);
            /*大概年龄是当前年减去出生年*/
            int age = yearNow - yearBirth;
            /*如果出当前月小与出生月，或者当前月等于出生月但是当前日小于出生日，那么年龄age就减一岁*/
            if (monthNow < monthBirth || (monthNow == monthBirth && dayNow < dayBirth)) {
                age--;
            }
            return age;
        } catch (Exception e) {//兼容性更强,异常后返回数据
            return 0;
        }
    }
}
